package com.zzx.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zzx.common.domain.CommonResult;
import com.zzx.pojo.Goods;
import com.zzx.service.IGoodsService;

import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @version 1.0
 * @Author ZZX
 * @Date 2023/10/3 12:35
 */
@Tag(name = "商品控制器", description = "商品模块")
@RestController
@RequestMapping("/goods")
public class GoodsController
{
    @Resource
    IGoodsService goodsService;

    /**
     * 获取商品信息
     *
     * @param goodsId
     * @return
     */
    @Tag(name = "商品控制器")
    @ApiOperation("获取商品信息")
    @HystrixCommand(fallbackMethod = "hystrixGetGoodsById")
    @GetMapping("/getGoodsById/{goodsId}")
    public CommonResult getGoodsById(@PathVariable("goodsId") Integer goodsId)
    {
        return goodsService.getGoodsById(goodsId);
    }

    public CommonResult hystrixGetGoodsById(@PathVariable("goodsId") Integer goodsId)
    {
        return CommonResult.error().message("没有匹配到相关数据！");
    }

    /**
     * 获取商品列表
     *
     * @return
     */
    @Tag(name = "商品控制器")
    @ApiOperation("获取商品列表")
    @HystrixCommand(fallbackMethod = "hystrixGetGoodsList")
    @GetMapping("/getGoodsList")
    public CommonResult getGoodsList()
    {
        return goodsService.getGoodsList();
    }

    public CommonResult hystrixGetGoodsList()
    {
        return CommonResult.error().message("没有匹配到相关数据！");
    }

    /**
     * 编辑商品数量
     *
     * @param goods
     * @return
     */
    @Tag(name = "商品控制器")
    @ApiOperation("编辑商品数量")
    @HystrixCommand(fallbackMethod = "hystrixEditGoodsNum")
    @PutMapping("/editGoodsNum")
    public CommonResult editGoodsNum(@RequestBody Goods goods)
    {
        return goodsService.editGoodsNum(goods);
    }

    public CommonResult hystrixEditGoodsNum(@RequestBody Goods goods)
    {
        return CommonResult.error().message("商品数量编辑失败，请联系客服！");
    }

}